﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using AspNetMvc.Entities;
using CoreVirtuoso;
using CoreVirtuoso.Config;
using CoreVirtuoso.Descriptions;
using NGenerics.DataStructures;

namespace WebGuiMvc.Helpers
{
   public static class HierarchyHelper
   {
      public static List<SelectListItem> GetHierarchyAsList(GeneralTree<EntityDescription> rootNode,
                                                            string valueToSelect, EventViewType viewType)
      {
         var list = new List<SelectListItem>();

         string dummyItemText = ViewStrings.SharedStrings.Choose;
         if (viewType == EventViewType.Search)
            dummyItemText = ViewStrings.SharedStrings.DoesntMatter;
         var dummyListItem = new SelectListItem { Value = DummyItemsConfig.DummyItemValue, Text = dummyItemText };
         if (String.IsNullOrEmpty(valueToSelect))
            dummyListItem.Selected = true;
         list.Insert(0, dummyListItem);

         string indent = "";
         GetHierarchyAsListRecursive(list, indent, rootNode.ChildNodes, valueToSelect);
         return list;
      }

      private static void GetHierarchyAsListRecursive(List<SelectListItem> list,
                                                      string indent,
                                                      IEnumerable<GeneralTree<EntityDescription>> nodes,
                                                      string valueToSelect)
      {
         foreach (GeneralTree<EntityDescription> node in nodes)
         {
            SelectListItem item = new SelectListItem
                                      {
                                         Value = node.Data.Uri.Fragment.Substring(1),
                                         Text = indent + node.Data.Label
                                      };
            if (!String.IsNullOrEmpty(valueToSelect) && node.Data.Uri.OriginalString == valueToSelect)
               item.Selected = true;

            list.Add(item);

            if (node.ChildNodes.Count != 0)
               GetHierarchyAsListRecursive(list, indent + "*", node.ChildNodes, valueToSelect);
         }
      }

      public static List<EntityDescription> GetHierarchyAsList(IEnumerable<GeneralTree<EntityDescription>> nodes)
      {
         List<EntityDescription> list = new List<EntityDescription>();
         string indent = "*";
         GetHierarchyAsListRecursive(list, indent, nodes);
         return list;
      }

      private static void GetHierarchyAsListRecursive(List<EntityDescription> list,
                                                      string indent,
                                                      IEnumerable<GeneralTree<EntityDescription>> nodes)
      {
         foreach (GeneralTree<EntityDescription> node in nodes)
         {
            node.Data.Label = String.Format("{0}{1}", indent, node.Data.Label);
            list.Add(node.Data);

            if (node.ChildNodes.Count != 0)
               GetHierarchyAsListRecursive(list, indent + "*", node.ChildNodes);
         }
      }
   }
}